mui.init();
mui.plusReady(function() {
var sessionId=(plus.navigator.getCookie("sessionId")==null)?"":plus.navigator.getCookie("sessionId");
	mui.getJSON("json/locat.json", function(data) {
		var locat = data["url"];
			var factoryid=data["factoryid"];
			var factoryId=(plus.navigator.getCookie("factoryid")==null)?"":plus.navigator.getCookie("factoryid");
					if(factoryId != factoryid)
					window.location.href="login.html";
		mui.ajax(locat + "/clientUser/driver_list?sessionId="+sessionId, {
			type: "GET",
			success: function(data, status, xhr) {
				if("error" == data["msg"])
					window.location.href = "login.html";
				else {
					var finallist = ""
					for(i = data["list_driver"].length - 1; i >= 0; i--) {
						finallist += "<tr>" +
							"<td>" + data["list_driver"][i]["name"] + "</td>" +
							"<td>" + data["list_driver"][i]["phone"] + "</td>" +
							" <td>" + data["list_driver"][i]["tracknumber"] + "</td>" +
							"  <td>" +
							"	<button type=\"button\" class=\"am-btn   am-btn-success am-btn-xs am-round\" onclick='updateDriver(\"" + data["list_driver"][i]["d_id"] + "\")'>修改</button>" +
							"	<button type=\"button\" class=\"am-btn am-fr  am-btn-primary am-btn-xs am-round\" onclick='delDriver(\"" + data["list_driver"][i]["d_id"] + "\")'>删除</button>" +
							"</td>" +
							" </tr>    ";
					}
					$("#table_driver").append(finallist);

				}

			},
			error: function(data) {
				plus.ui.toast("","服务异常！","error");
				window.location.href=login.html;
			}
		});
	});

});

//删除driver
function delDriver(driverid) {
	var sessionId=(plus.navigator.getCookie("sessionId")==null)?"":plus.navigator.getCookie("sessionId");
	swal({
		title: "您确定要删除吗？",
		text: "",
		type: "warning",
		showCancelButton: true,
		cancelButtonText: "取消",
		closeOnConfirm: false,
		confirmButtonText: "确定",
		confirmButtonColor: "#ec6c62"
	}, function() {
		mui.getJSON("json/locat.json", function(data) {
			var locat = data["url"];
				var factoryid=data["factoryid"];
			var factoryId=(plus.navigator.getCookie("factoryid")==null)?"":plus.navigator.getCookie("factoryid");
					if(factoryId != factoryid)
					window.location.href="login.html";
			mui.ajax(locat + "/clientUser/delDriver?driverid=" + driverid+"&sessionId="+sessionId, {
				type: "GET",
				success: function(data, status, xhr) {
					if(data["mes"] == "success") {
						swal("Good!", "删除成功", "success"); 
						window.location.href = "tjsj.html";
					} else {
						swal("此司机还有订单，不能删除", "", "error"); 
					}
				},
				error: function(data) {
					plus.ui.toast("","服务异常","error");
				    window.location.href=login.html;
				}
			});
		});

	});

}

//修改driver
function updateDriver(driverid) {

	window.location.href = "xgsja.html?driverid=" + driverid;
}

function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return unescape(r[2]);
	return null;
}